﻿using AutoMapper;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Production_Modules;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Production_Modules
{
    /// <summary>
    /// 物料库存修改命令
    /// </summary>
    public class InventoryUpdateCommandHandler : IRequestHandler<InventoryUpdateCommand, ApiResult<int>>
    {
        /// <summary>
        /// 物料库存
        /// </summary>
        IRepository<MaterialInventoryModel> _inventoryRepository;
        /// <summary>
        /// Mappering
        /// </summary>
        IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="inventoryRepository">物料库存</param>
        /// <param name="mapper">Mappering</param>
        public InventoryUpdateCommandHandler(IRepository<MaterialInventoryModel> inventoryRepository,IMapper mapper)
        {
            _inventoryRepository = inventoryRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(InventoryUpdateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code=StatusCode.Success;
            result.Message=StatusCode.Success.ToString();

            MaterialInventoryModel model = _mapper.Map<MaterialInventoryModel>(request);
            await _inventoryRepository.UpdateAsync(model);
            return await Task.FromResult(result);
        }
    }
}
